package com.lili.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.controller.request.XasCouponListRequest;
import com.lili.controller.request.XasUserCouponRecordsRequest;
import com.lili.mapper.XasCouponListMapper;
import com.lili.mapper.XasUserCouponRecordsMapper;
import com.lili.po.XasCouponList;
import com.lili.uitl.ShiroUtils;
import com.lili.vo.XasUserCouponRecordsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class XasCouponService {
    @Autowired
    XasUserCouponRecordsMapper xasUserCouponRecordsMapper;
    @Autowired
    XasCouponListMapper xasCouponListMapper;
    @Autowired
    UserService userService;


    //实名认证  按照手机号，姓名，状态查询
    public IPage<XasUserCouponRecordsVo> xasUserCouponRecords(XasUserCouponRecordsRequest xasUserCouponRecordsRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasUserCouponRecordsRequest, page);
        IPage iPage = xasUserCouponRecordsMapper.xasUserCouponRecords(page, xasUserCouponRecordsRequest.getPhone(), xasUserCouponRecordsRequest.getStatus());
        return iPage;
    }

    @Transactional
    public void addCoupon(XasCouponList xasCouponList) {
        String userLoginName = ShiroUtils.getUserLoginName(userService);
        xasCouponList.setCreateUser(userLoginName);
        xasCouponList.setCreateTime(new Date());
        xasCouponListMapper.insert(xasCouponList);
    }

    @Transactional
    public void delCoupon(Integer id) {
        xasCouponListMapper.deleteById(id);
    }

    @Transactional
    public void updateCoupon(XasCouponList xasCouponList) {
        xasCouponListMapper.updateById(xasCouponList);
    }


    public IPage<XasCouponList> xasCouponList(XasCouponListRequest xasCouponListRequest) {
        Page page = new Page();
        BeanUtils.copyProperties(xasCouponListRequest, page);
        LambdaQueryWrapper<XasCouponList> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(xasCouponListRequest.getName()), XasCouponList::getName, xasCouponListRequest.getName());
        lambdaQueryWrapper.eq(xasCouponListRequest.getStatus() != null, XasCouponList::getStatus, xasCouponListRequest.getStatus());
//        IPage<XasCouponList> iPage = xasCouponListMapper.selectPage(page, lambdaQueryWrapper);
        IPage<XasCouponList> iPage =
                xasCouponListMapper.xasCouponList(page, xasCouponListRequest.getName(), xasCouponListRequest.getStatus());
        return iPage;
    }


}
